<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="categoryName" placeholder="请输入学习分类名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="categoryOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
          <el-form-item label="学习库名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入学习库名称" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['customer:learn:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
              v-hasPermi="['customer:learn:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['customer:learn:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['customer:learn:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="learnList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="学习库名称" align="center" prop="name" />
          <el-table-column label="学习类型" align="center" prop="learnTypeName" />
          <el-table-column label="学习库描述" align="center" prop="description" />
          <el-table-column label="学习库图片地址" align="center" prop="pictureUrl" />
          <el-table-column label="学习库状态" align="center" prop="statusName" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['customer:learn:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['customer:learn:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改学习库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学习名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入学习库名称" />
        </el-form-item>
        <el-form-item label="上下架" prop="status">
          <el-radio-group v-model="form.status" size="medium">
            <el-radio v-for="(item, index) in statusOption" :key="index" :label="item.value" :disabled="item.disabled">{{
              item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学习类型">
          <el-select v-model="form.learnType" placeholder="请选择类型">
            <el-option v-for="dict in dict.type.learn_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片连接" prop="pictureUrl">
          <ImageUpload @success="uploadPicture" :limit="1" :value="form.pictureUrl" />
        </el-form-item>
        <el-form-item v-if="form.learnType == 'JumpURL' || form.learnType == 'LearnVideo'" label="连接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入学习库连接地址" />
        </el-form-item>
        <el-form-item v-if="form.learnType == 'LearnVideo'" label="视频类型">
          <el-select v-model="form.videoType" placeholder="请选择类型">
            <el-option v-for="dict in dict.type.learn_video_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.learnType == 'LearnVideo'" label="视频时长" prop="videoTime">
          <el-input-number v-model="form.videoTime" :min="1" :max="100000" label="时长数值"></el-input-number>
          <!-- <el-input v-model="form.videoTime" placeholder="请输入视频长度" /> -->
        </el-form-item>
        <el-form-item v-if="form.learnType == 'RichText'" label="内容富文本">
          <editor v-model="form.learnContent" :min-height="192" :value="richText" @on-change="editorChange" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLearn, getLearn, delLearn, addLearn, updateLearn } from "@/api/customer/learn";
import { getLearncategoryTree } from "@/api/customer/learncategory";

export default {
  name: "Learn",
  dicts: ['learn_type','learn_video_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习库表格数据
      learnList: [],
      categoryOptions: [],
      richText: "",
      defaultProps: {
        children: "children",
        label: "label"
      },
      selectCategoryId: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //学习库分类名称
      categoryName: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        learnCategoryId: null,
        name: null,
        learnType: null,
        videoType: null,
        videoTime: null,
        description: null,
        pictureUrl: null,
        url: null,
        learnContent: null,
        status: null,
      },
      statusOption: [{
        "label": "下架",
        "value": "DownShelves"
      }, {
        "label": "上架",
        "value": "UpShelves"
      }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    categoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getLearnCategoryTreeList();
  },
  methods: {
    /** 查询学习库列表 */
    getList() {
      this.loading = true;
      listLearn(this.queryParams).then(response => {
        this.learnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    editorChange(param) {
      this.richText = param.html;
    },
    /** 查询部门下拉树结构 */
    getLearnCategoryTreeList() {
      getLearncategoryTree().then(response => {
        this.categoryOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        learnId: null,
        learnCategoryId: null,
        name: null,
        learnType: null,
        videoType: null,
        description: null,
        pictureUrl: null,
        url: null,
        learnContent: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    uploadPicture(file) {
      this.form.pictureUrl = file;
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.learnCategoryId = data.id;
      this.selectCategoryId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.learnId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //上下架状态默认为上架
      this.form.status = 1;
      this.open = true;
      this.title = "添加学习库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const learnId = row.learnId || this.ids
      getLearn(learnId).then(response => {
        this.form = response.data;
        this.richText = this.form.learnContent;
        this.open = true;
        this.title = "修改学习库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      debugger;
      if (this.form.status == null) {
        this.$modal.msgError("请选择上架下架状态");
        return;
      }
      if (this.selectCategoryId == null) {
        this.$modal.msgError("请选择学习库分类");
        return;
      }
      if (this.form.name == null || this.form.name == "") {
        this.$modal.msgError("请填写学习名称");
        return;
      }
      if (this.form.learnType == null || this.form.learnType == "") {
        this.$modal.msgError("请选择学习类型");
        return;
      }
      if (this.form.learnType == 0 || this.form.learnType == 1) {
        if (this.form.pictureUrl == null || this.form.pictureUrl == "") {
          this.$modal.msgError("请填写图片连接");
          return;
        }
      }
      if (this.form.learnType == 2) {
        if (this.form.url == null || this.form.url == "") {
          this.$modal.msgError("请填写地址跳转连接");
          return;
        }
      }
      this.form.learnContent = this.richText;
      this.form.learnCategoryId = this.selectCategoryId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.learnId != null) {
            updateLearn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLearn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const learnIds = row.learnId || this.ids;
      this.$modal.confirm('是否确认删除学习库编号为"' + learnIds + '"的数据项？').then(function () {
        return delLearn(learnIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/learn/export', {
        ...this.queryParams
      }, `learn_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
